<template>
  <div id="carrierGroup" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" :title="$t('bill.carrierGroup')" @del="del" @newCreate="newCreate" @exportFill="exportFill"></v-tableTitle>
    <v-table :pageSize="pageSize" :page="page" :tableConfig="tableConfig" :tableData="tableData" :total="total" @findData="getTableData" @getTableData="getTableData" @searhData="searhData"
      @selectData="selectData" @setdialog="setdialog" @upData="getTableData" ref="table"></v-table>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from 'commons/TableTitle'
import table from 'commons/Table'
import baseMixin from '@/mixins/BaseMixin'
import config from './Config'
import bus from '../../../../bus/bus'

export default {
  name: 'carrierGroup',
  mixins: [baseMixin, config],
  data() {
    return {
      loading: false,
      tableConfig: [
        {
          label: this.$t('bill.carrierGroup'),
          prop: 'carrier_group',
          input: 'input'
        },
        {
          label: this.$t('bill.carrierGroupDesc'),
          prop: 'carrier_group_desc',
          input: 'input'
        }
      ],
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 0,
      tableSelectData: [],
      tableSearhData: {},
      newStatus: true,
      permissionStatus: false,
      wms: ''
    }
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.create'), invalid: 'newCreate' },
        { label: this.$t('common.delete'), disabled: true, invalid: 'del' },
        {
          label: this.$t('common.export'),
          disabled: true,
          invalid: 'exportFill'
        }
      ]
    }
  },
  created() {},
  methods: {
    async exportFill(data) {
      try {
        let res = await this.$api.post('/downCarrierGroup', data, {
          responseType: 'blob'
        })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data])
          window.navigator.msSaveBlob(blobObject, 'carriergroup.xls')
        } else {
          var blob = new Blob([res.data], {
            type:
              'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'
          })
          var filename = 'carriergroup.xls'
          var downloadElement = document.createElement('a')
          var href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.style.display = 'none'
          downloadElement.href = href
          downloadElement.download = filename //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e)
      }
    },
    //新建
    async newCreate() {
      this.$store.commit('setNewFlag', true)
      this.$router.push({ name: 'carrierGroupDeawer' })
    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data
    },
    //删除
    async del(data) {
      try {
        let res = await this.$api.post(`/deleteCarrierGroup`, data)

        this.$message.success(res.returnMgr)
        let page = sessionStorage.getItem('page')
        this.getTableData(page, this.tableSearhData)
      } catch (e) {
        this.$message.error(e)
      }
    },
    async getTableData(page, data) {
      try {
        this.loading = true
        let res = await this.$api.post(
          `/queryCarrierGroupList/${this.wms}/${page}`,
          data
        )

        this.tableData = res.page.list
        this.pageSize = res.page.pageSize
        this.page = res.page.curPage
        this.total = res.page.total
      } catch (e) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data
      } else {
        this.tableSearhData = data
        this.getTableData(1, this.tableSearhData)
      }
    },
    //设置弹框
    setdialog(data) {
      this.$store.commit('setRouteData', data)
      this.$router.push({ name: 'carrierGroupDeawer' })
    }
  },
  mounted() {
    this.wms = this.$store.state.wms
    this.$store.commit('setSkip', {})
    this.$store.commit('setNewFlag', false)
    this.$store.commit('setRouteData', {})
    this.getTableData(1, {})
  },
  watch: {
    tableSelectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteStatus = false
        } else {
          this.deleteStatus = true
        }
      }
    }
  },
  components: {
    'v-tableTitle': tableTitle,
    'v-table': table
  }
}
</script>

<style scoped>
#carrierGroup {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
